package com.ssy.lingxi.product.repository.customer;

import com.ssy.lingxi.product.entity.customer.CustomerAttribute;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
 * 属性持久化层
 * @author jw.chen
 * @version 2.0.0
 * @date 2020/6/22
 */
public interface CustomerAttributeRepository extends JpaRepository<CustomerAttribute,Long>, JpaSpecificationExecutor<CustomerAttribute> {
    Page<CustomerAttribute> findByMemberIdAndMemberRoleIdAndNameContainingOrderByIdDesc(Long memberId, Long memberRoleId, String name, Pageable page);

    Page<CustomerAttribute> findByMemberIdAndMemberRoleIdAndNameContainingAndCustomerCategoryListId(Long memberId, Long memberRoleId, String name, Long categoryId, Pageable page);

    List<CustomerAttribute> findByMemberIdAndMemberRoleIdAndNameContainingAndCustomerCategoryListId(Long memberId, Long memberRoleId, String name, Long categoryId);

    List<CustomerAttribute> findByNameContainingAndCustomerCategoryListId(String name, Long categoryId);

    List<CustomerAttribute> findByNameContainingAndCustomerCategoryListIdIn(String name, List<Long> categoryId);

    List<CustomerAttribute> findByMemberIdAndMemberRoleIdAndNameAndIdNot(Long memberId, Long memberRoleId, String name, Long id);

    List<CustomerAttribute> findByMemberIdAndMemberRoleIdAndName(Long memberId, Long memberRoleId, String name);

    List<CustomerAttribute> findByMemberIdAndMemberRoleIdAndIsEnable(Long memberId, Long memberRoleId, Boolean isEnable);

    List<CustomerAttribute> findByMemberIdAndMemberRoleIdAndIsEnableAndTypeNot(Long memberId, Long memberRoleId, Boolean isEnable, Integer type);

    List<CustomerAttribute> findByMemberIdAndMemberRoleIdAndIdIn(Long memberId, Long memberRoleId, List<Long> attributeIdList);

    //平台后台调用
    List<CustomerAttribute> findByAttributeId(Long attributeId);

    List<CustomerAttribute> findByAttributeIdIn(List<Long> attributeIdList);

    List<CustomerAttribute> findByIdIn(List<Long> customerAttributeIdList);

    Page<CustomerAttribute> findByMemberIdAndMemberRoleIdAndNameContainingAndIdNotIn(Long memberId, Long memberRoleId, String name, List<Long> idList, Pageable page);

    Page<CustomerAttribute> findByMemberIdAndMemberRoleIdAndNameContaining(Long memberId, Long memberRoleId, String name, Pageable page);

    List<CustomerAttribute> findByMemberIdAndMemberRoleIdAndNameIn(Long memberId, Long memberRoleId, List<String> attributeNameList);

    @Query(value = "select id from product_customer_attribute_value \n" +
            "where customer_attribute_id \n" +
            "in (select attribute_id from product_customer_category_attribute \n" +
            "where category_id in (:customerCategoryIdList))",nativeQuery = true)
    List<Long> findAttributeValueId(List<Long> customerCategoryIdList);

}
